//
//  HWGameCircleProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/30.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWGameCircleProtocol: NSObject {
    var modelArr:[HWGameCircleModel]?
    var groupModelArr:[HWGameCircleGroupModel]?{
        didSet{
            if groupModelArr?.count ?? 0 > 0 {
                hasGroup = 1
            }else {
                hasGroup = 0
            }
        }
    }
    var hasGroup = 0
    /// 点击图片
    var didSelectImage:((_:Int,_:[String])->())?
    /// 点击视频
    var didSelectVideo:((_:String)->())?
    var zanButtonClick:((_:String)->())?
    var jumpToDetail:((_:HWGameCircleModel)->())?
    var jumpToUserDetail:((_:String)->())?
    
    /// 加入群组
    var jumpToGroup:((_:String)->())?
    /// 跳转游戏
    var jumpToGameDetail:((_:String)->())?
    
}
extension HWGameCircleProtocol: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if hasGroup == 1 {
            if indexPath.section == 0 {
                
            }else{
                if modelArr?[indexPath.section-hasGroup].isRecommend == true {
                    self.jumpToGameDetail?(modelArr?[indexPath.section-hasGroup].id ?? "")
                } else if modelArr?[indexPath.section - hasGroup].video.count ?? 0 > 0 {
                    didSelectVideo?((modelArr?[indexPath.section - hasGroup].video)!)
                }else{
                    didSelectImage?(indexPath.row,modelArr?[indexPath.section - hasGroup].img ?? [])
                }
            }
        }else{
            if modelArr?[indexPath.section-hasGroup].isRecommend == true {
                self.jumpToGameDetail?(modelArr?[indexPath.section-hasGroup].id ?? "")
            } else if modelArr?[indexPath.section - hasGroup].video.count ?? 0 > 0 {
                didSelectVideo?((modelArr?[indexPath.section - hasGroup].video)!)
            }else{
                didSelectImage?(indexPath.row,modelArr?[indexPath.section - hasGroup].img ?? [])
            }
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 0 {
            if hasGroup == 1 {
                return UIEdgeInsets.zero
            }else {
                if modelArr?[section-hasGroup].isRecommend == true {
                    return UIEdgeInsets.zero
                }
                
                if modelArr?[section - hasGroup].img.count == 1 {
                    return UIEdgeInsets(top: 0, left: 76*widthScale, bottom: 0, right: 28*widthScale)
                } else {
                    return UIEdgeInsets(top: 0, left: 76*widthScale, bottom: 0, right: 28*widthScale)
                }
            }
        }else{
            if modelArr?[section-hasGroup].isRecommend == true {
                return UIEdgeInsets.zero
            }
            if modelArr?[section - hasGroup].img.count == 1 {
                return UIEdgeInsets(top: 0, left: 76*widthScale, bottom: 0, right: 28*widthScale)
            } else {
                return UIEdgeInsets(top: 0, left: 76*widthScale, bottom: 0, right: 28*widthScale)
            }
        }
        
    }
        
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if indexPath.section == 0 {
            if hasGroup == 1 {
                return UIView() as! UICollectionReusableView
            }
        }
        if modelArr?[indexPath.section-hasGroup].isRecommend == true {
            return UIView() as! UICollectionReusableView
        }
        if kind == UICollectionView.elementKindSectionHeader {
            let  header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWGameCircleHeader", for: indexPath) as! HWGameCircleHeader
            header.model = modelArr?[indexPath.section - hasGroup]
            
            header.jumpToDetail = {
                self.jumpToDetail!($0)
            }
            
            header.jumpToUserDetail = {
                self.jumpToUserDetail!($0)
            }
            
            return header
            
        }else{
            let  footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWGameCircleFooter", for: indexPath) as! HWGameCircleFooter
            footer.model = modelArr?[indexPath.section - hasGroup]
            footer.zanButtonClick = {
                self.zanButtonClick!($0)
            }
            
            
            footer.jumpToDetail = {
                self.jumpToDetail!($0)
            }
            
            return footer
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
    {
        if hasGroup == 1 {
            if section == 0 {
                return CGSize.zero
            }else {
                if modelArr?[section-hasGroup].isRecommend == true {
                    return CGSize.zero
                }
                return CGSize(width: screenWidth, height: getTextHeigh(textStr: modelArr?[section-hasGroup].content ?? "", font: .systemFont(ofSize: 13*widthScale), width: screenWidth-97*widthScale) + 90*widthScale)
            }
        }else {
            if modelArr?[section-hasGroup].isRecommend == true {
                return CGSize.zero
            }
            return CGSize(width: screenWidth, height: getTextHeigh(textStr: modelArr?[section-hasGroup].content ?? "", font: .systemFont(ofSize: 13*widthScale), width: screenWidth-97*widthScale) + 82*widthScale)
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        if hasGroup == 1 {
            if section == 0 {
                return CGSize.zero
            }else {
                if modelArr?[section-hasGroup].isRecommend == true {
                    return CGSize.zero
                }
                return CGSize(width: screenWidth, height: 46*widthScale)
            }
        }else {
            if modelArr?[section-hasGroup].isRecommend == true {
                return CGSize.zero
            }
            return CGSize(width: screenWidth, height: 75*widthScale)
        }
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if hasGroup == 1 {
            if indexPath.section == 0 {
                return CGSize (width: 375*widthScale, height: 148*widthScale)
            }
        }
        
        if modelArr?[indexPath.section-hasGroup].isRecommend == true {
            return CGSize(width: screenWidth, height: 100*widthScale)
        }
        
        if modelArr?[indexPath.section-hasGroup].video.count ?? 0 > 0 {
            return CGSize (width: 295*widthScale, height: 152*widthScale)
        }
        if modelArr?[indexPath.section-hasGroup].img.count ?? 0 == 1 {
            return CGSize (width: 295*widthScale, height: 152*widthScale)
        }else if modelArr?[indexPath.section-hasGroup].img.count ?? 0 == 2{
            
            return CGSize (width: (CGFloat.screenWidth - 106.0*widthScale)/2, height: 85*widthScale)
        }else{
            
            return CGSize (width: (CGFloat.screenWidth - 110.0*widthScale)/3, height: (CGFloat.screenWidth - 110.0*widthScale)/3)
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return (modelArr?.count ?? 0) + hasGroup
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if hasGroup == 1 {
            if section == 0 {
                return 1
            }
        }
        if modelArr?[section - hasGroup].isRecommend == true {
            return 1
        }
        
        if modelArr?[section - hasGroup].video.count ?? 0 > 0 {
            return 1
        }else{
            return modelArr?[section - hasGroup].img.count ?? 0
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let pageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameCircleCell", for: indexPath) as! HWGameCircleCell
        let topCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameCircleTopCell", for: indexPath) as! HWGameCircleTopCell
        let recommendCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameCircleRecommendCell", for: indexPath) as! HWGameCircleRecommendCell
        
        if hasGroup == 1 {
            if indexPath.section == 0{
                topCell.groupModelArr = self.groupModelArr
                topCell.jumpToGroup = {
                    self.jumpToGroup?($0)
                }
                return topCell
            }
            
        }
        
        if modelArr?[indexPath.section - hasGroup].isRecommend == true {
            recommendCell.model = modelArr?[indexPath.section - hasGroup]
            return recommendCell
        }
        
        
        pageCell.model = modelArr?[indexPath.section - hasGroup]
        if modelArr?[indexPath.section - hasGroup].video.count ?? 0 > 0{
            
        }else{
            
            pageCell.imageView.sd_setImage(with: URL(string: modelArr?[indexPath.section - hasGroup].img[indexPath.row] ?? ""), completed: nil)
        }
        pageCell.layoutIfNeeded()
        if modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 1 {
            pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 8*widthScale)
        }else if modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 2 {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft], radius: 8*widthScale)
            }else {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else if modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 3 {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight,.bottomRight], radius: 8*widthScale)
            }else {
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else if modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 4 || modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 5{
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 3 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
            
        }else if modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 6 {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 3 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else if indexPath.row == 5 {
                pageCell.imageView.addCorner(conrners: [.bottomRight], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else if modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 7 || modelArr?[indexPath.section - hasGroup].img.count ?? 0 == 8{
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 6 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }else {
            if indexPath.row == 0 {
                pageCell.imageView.addCorner(conrners: [.topLeft], radius: 8*widthScale)
            }else if indexPath.row == 2{
                pageCell.imageView.addCorner(conrners: [.topRight], radius: 8*widthScale)
            }else if indexPath.row == 6 {
                pageCell.imageView.addCorner(conrners: [.bottomLeft], radius: 8*widthScale)
            }else if indexPath.row == 8 {
                pageCell.imageView.addCorner(conrners: [.bottomRight], radius: 8*widthScale)
            }else{
                pageCell.imageView.addCorner(conrners: [.topLeft,.bottomLeft,.topRight,.bottomRight], radius: 0)
            }
        }
        
        return pageCell
    }
    
    
}
